<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------
/**
 *  Entrance File of the Control class.
 *
 * @Author:    slash<phpunion@gmail.com>
 * @Package:   Interface of the View class. 
 * @Version:   $Id: info_guest.class.php 2012-04-24 19:19:19 $
 * @Show Note: 客户管理
 */
Class info_guest extends TheAdmin
{
		/* DataBase object */
		public $db;
	
		/* View the data page limit items */
		public $pageSize = 10;
		
		/**
		 *	Construct this module class.
		*/
		public function info_guest()
		{
		}
		public function set()
		{
			$this->TheAdmin();
			//定义模版
			$this->tplFile ="info_guest.tpl";
			$this->admin_user_id = $this->userRequest->get('admin_user_id');
			
			//获得用户相关信息
			$this->user_info = $this->get_user_info($this->admin_user_id);
			//取得数据库连接
			$this->db = Factory::getDB("info_guest");
			//取得用户对于该类的权限
			$user_p = $this->get_user_popedom_by_class_name(__CLASS__);
			$class_p_arr = $this->show_p();
			//取得该类的权值数组
			$class_method = $class_p_arr['class_method'];
			//取得用户操作菜单
			$menu = array();
			foreach ($class_method as $k=>$v)
			{
				if( (parse_power($user_p,$v[2]) && (isset($v[3]) ?$v[3] :false)) || IS_ROOT)
				{
					array_push($menu,array('url'=>"?c=".__CLASS__."&m={$v[0]}",'title'=>$v[1]));
				}
			}
			//获得所有部门
			$this->smarty->assign('company_arr',$this->get_company_arr());
			//用户有权菜单生成 可以添加其他操作
			$this->smarty->assign('menus',$menu);
			$this->smarty->assign("main_menus",$this->get_user_menus());
		}
	
		/**
		 *	Call class construct method.
		*/
		public function __construct()
		{
				$this->info_guest();
		}
		public function _display()
		{
			$this->smarty->display($this->tplFile);
		}
		/**
		 *	default method.
		*/
		public function show()
		{
			$ord = isset($_GET['ord']) ? trim($_GET['ord']) : '';
			if('' == $ord) {
				$col = "guest_id";
			} else {
			    $col =  $ord;

				$co = isset($_GET['co']) ? intval($_GET['co']) : 0;
				if($co == 0) {
					$this->db->setOrderBy();
					$co = 1;
				} else {
				    $co = 0;
				}
				$this->smarty->assign('co',$co);
			}
				
				//$where = 1;
				if($this->user_info['is_admin']){
					$where = 1;
				}else{
					$where = "admin_user_id={$this->admin_user_id}";
					if($this->user_info['is_depart']){
						$where .= " or guest_department_tree like '%,{$this->user_info['is_depart']},%'";
					}
				}
				//echo $where;
				
				$rowNo = $this->db->getSumNumber($this->db->tableName,$where);
				//数据库执行sql语句出错
				//取得错误的sql语句
				//$error_sql = end($this->db->getDebugMsg());
				//echo $error_sql;
				if( 0 > $rowNo) goToIndex($rowNo);
				//分页
				$page = Factory::getBaseClass('pagination');
				$page->set($rowNo,$this->pageSize);
				$numrows = $this->pageSize;
				$offset  = $page->getSqlMove();
				$arr = $this->db->getAll($where,$col,$numrows,$offset);
				admin_log($this->admin_user_id,__METHOD__,'查看',1);
				//数据库执行select语句出错
				//取得错误的sql语句
				//$error_sql = end($this->db->getDebugMsg());
				//echo $error_sql;
				if(0 > $arr ) goToIndex($arr);
				
				$this->smarty->assign('arr',$arr);
				//默认方法
				$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
				$this->smarty->assign('curPage',$page->result['curPage']);
				$this->smarty->assign('act','list');
				$this->smarty->display($this->tplFile);
		}
		
		/**
		 *	add method.
		*/
		public function add()
		{
				//添加数据
				if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
				{
						//使用模版，展示提交界面
						$this->smarty->assign('act_title','添加');
						$this->smarty->assign('act','add');
						$this->smarty->display($this->tplFile);
				} else {
						//用户提交，把数据插入数据库
						$temp = $this->getValue();
						$insrt = $this->db->insert_info_guest($temp);
						$error_no = 0 > $insrt ?  $insrt :'-1501';
						admin_log($this->admin_user_id,__METHOD__,"id={$insrt}",2);
						//取得错误的sql语句
						//$error_sql = end($this->db->getDebugMsg());
						//echo $error_sql;
						alertMsg($error_no, 'index.php?c=info_guest');
				}
		}
		
		/**
		 *	edit method.
		*/
		public function edit()
		{
				//取得id，也就是唯一索引
				$guest_id = isset($_REQUEST['guest_id']) ? intval(trim($_REQUEST['guest_id'])) : 0 ;
				//取得是否查看详情
				$is_show = isset($_REQUEST['is_show']) ? intval(trim($_REQUEST['is_show'])) : 0 ;
				if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
				{
						//使用模版，展示提交界面
						$result = $this->db->getOne($guest_id);
						if(empty($result))
						{
							alertMsg('-2001', 'index.php?c=info_guest');
						}
						foreach ($result AS $k=>$v)
						{
							$this->smarty->assign("{$k}",$v);
						}

						//---- 相关人列表开始
						$col = "other_id";	
						$where = "guest_id='{$guest_id}'";
						$db_obj = Factory::getDB("v_guest_other");
						$guest_list = $db_obj->getAll($where,$col);
						$this->smarty->assign('guest_list',$guest_list);
						$this->smarty->assign('guest_id',$guest_id);
						//-----相关人列表结束

						$this->smarty->assign('is_show',$is_show);
						$this->smarty->assign('act_title','修改');
						$this->smarty->assign('act','edit');
						$this->smarty->display($this->tplFile);
				} else {
						//用户提交，把数据插入数据库
						//设置where条件
						$where = "guest_id=$guest_id";
						$temp = $this->getValue(1);
						$update = $this->db->update_info_guest($temp,$where);
						admin_log($this->admin_user_id,__METHOD__,$where,2);
						$error_no = 0 > $update ?  $update :'-1502';
						//取得错误的sql语句
						//$error_sql = end($this->db->getDebugMsg());
						//echo $error_sql;
						alertMsg($error_no, 'index.php?c=info_guest');
				}
		}
		
		/**
		 *	del method.
		*/
		public function del()
		{
				//取得id，也就是唯一索引
				$guest_id = isset($_REQUEST['guest_id']) ? intval(trim($_REQUEST['guest_id'])) : 0 ;
				$where = "guest_id=$guest_id";
				$del=$this->db->delete_info_guest($where);
				admin_log($this->admin_user_id,__METHOD__,$where,3);
				$error_no = 0 > $del ?  $del :'-1503';
				//取得错误的sql语句
				//$error_sql = end($this->db->getDebugMsg());
				//echo $error_sql;
				alertMsg($error_no, 'index.php?c=info_guest');
		}
		//图片上传
		public function upload_pic($user_id){
			$db = Factory::getDb('info_image');
			$user_link_db = Factory::getDb('info_user_image');
			$io = Factory::getBaseClass('FileIO');
			foreach ($_FILES["pictures"]["error"] as $key => $error) {  
			     if ($error == UPLOAD_ERR_OK) {  
			         $tmp_name = $_FILES["pictures"]["tmp_name"][$key];  
			         //$name    = $_FILES["pictures"]["name"][$key];  
			         $name = 'user'.PATH.$user_id.PATH.time().'_'.$key . '.' . substr($_FILES["pictures"]["name"][$key],-3,3);
			         $image_arr = array('image_url'=>$name);
					 $uploadfile = UPLOAD_PATH.$name;  
			         //move_uploaded_file($tmp_name, $uploadfile);  
					 $rs = $io->copyFile($tmp_name,$uploadfile);
					//print_r($rs);
					$image_id = $db->insert_info_image($image_arr);
					if($image_id){
						$link_arr = array('admin_user_id'=>$user_id,'image_id'=>$image_id);
						$user_link_db->insert_info_user_image($link_arr);
					}
			     }
			 }
		
			
		}
		/**
		 *	get request method.
		*/
		public function &getValue($isEdit=0)
		{
			$temp = array();
			$check = Factory::getBaseClass("checkBase");
			if($isEdit){
				$temp['guest_id'] = $_POST['guest_id'];
			} else {
				$temp['guest_id'] = "";
				$temp['admin_user_id'] = $this->admin_user_id;
				$user_arr = $this->get_user_info($this->admin_user_id);
				//print_r($user_arr);exit;
				$temp['guest_department_tree'] = $user_arr['user_department_tree'];
			}
			//补充其他的字段
			$temp['guest_name'] = $_POST['guest_name'];
			$temp['guest_en_name'] = $_POST['guest_en_name'];
			$temp['guest_birthday'] = $_POST['guest_birthday'];
			$temp['guest_sex'] = $_POST['guest_sex'];
			$temp['guest_mob'] = $_POST['guest_mob'];
			$temp['guest_tel'] = $_POST['guest_tel'];
			$temp['guest_mail'] = $_POST['guest_mail'];
			$temp['guest_qq'] = $_POST['guest_qq'];
			$temp['guest_msn'] = $_POST['guest_msn'];
			$temp['guest_sns'] = $_POST['guest_sns'];
			$temp['guest_marry'] = $_POST['guest_marry'];			
			$temp['guest_wedding'] = $_POST['guest_wedding']; //结婚纪念日
			$temp['children_birthdays'] = $_POST['children_birthdays']; //孩子的生日			
			$temp['guest_sfz'] = $_POST['guest_sfz'];
			$temp['guest_home'] = $_POST['guest_home'];
			$temp['guest_home_tel'] = $_POST['guest_home_tel'];
			$temp['guest_home_zip'] = $_POST['guest_home_zip'];
			$temp['guest_work_info'] = $_POST['guest_work_info'];
			$temp['guest_intree_info'] = $_POST['guest_intree_info'];
			$temp['guest_home_info'] = $_POST['guest_home_info'];
			$temp['guest_studio_info'] = $_POST['guest_studio_info'];
			$temp['guest_info'] = $_POST['guest_info'];
			
			$temp['laster'] = $this->admin_user_id;
			$temp['update_time'] = time();
			
			$temp['company_id'] = $_POST['company_id'];
			admin_log($this->admin_user_id,__METHOD__,serialize($temp),2);	
			return $temp;
		}
		/**
		 *	get class method and popedom.
		*/
		public function show_p()
		{
			//该方法为，提供用户使用方法定义，非web用户权限相关不用写
			return array(
				/* 定义该类中文名称*/
				'class_name'=>array(__CLASS__,'客户管理'),
				'class_method' =>array(
					//array（方法名，中文解释，权值,是否为菜单）
					'show'  => array('show','列表',1,true),
					'add'  => array('add','添加',2,true),
					'edit'  => array('edit','编辑',4,false),
					'del'  => array('del','删除',8,false),
					'show_p'=> array('show_p','',0,false)
				)
			);
		}
}

?>